from threading import Lock
import requests
from lxml import etree
import re
import parsel
import pymysql
import datetime
import http.client
import threading
lock = Lock()
http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
http.client.HTTPConnection._http_vsn = 10
conn = pymysql.connect(host='14.29.212.205', port=8306, user='asinuser', password='16Asin@~!1^g902',
                       database='sun_read', charset='utf8')
cursor = conn.cursor()
data = {
    'order_by': '',
    'fee_type': '',
    'E10': '',
    'E44': '',
    'E6': '',
    'E12': '',
    'E11': '',
    'code_type': 'stpo_code',
    'code': '',
    'sku_type': 'sku_like',
    'sku_code': '',
    'E13': '',
    'user_account': '',
    'remark': '',
    'timeType': '',
    'dateFor': '',
    'dateTo': '',
    'confirm_dateFor': '',
    'confirm_dateTo': '',
}
headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control': 'no-cache',
    'Content-Length': '184',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'UM_distinctid=17c15b16b7275-02b55430879ff9-2343360-1fa400-17c15b16b7376d; _ga=GA1.2.368327117.1632464693; LANGUAGE=zh_CN; PHPSESSID=s4rspra1rp4omskbsk55krte96; SERVERID=c2e5cdc45974b52df44ef6e66ee49d8b|1638505447|1638159244; currentPage=380%7B%7C%7D%2Ftransfer%2Fservice-transfer-plan-order%2Flist%3Fquick%3D380%7B%7C%7D%E6%B5%B7%E5%A4%96%E4%BB%93%E5%A4%B4%E7%A8%8B%E5%8F%91%E8%B4%A7%E7%AE%A1%E7%90%86',
    'Host': 'sw.eccang.com',
    'Origin': 'http://sw.eccang.com',
    'Pragma': 'no-cache',
    'Proxy-Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',

}


def data0():
    # http://sw.eccang.com/transfer/service-transfer-plan-order/list/page/1/pageSize/100
    url = 'http://sw.eccang.com/transfer/service-transfer-plan-order/list/page/1/pageSize/20'
    res = requests.post(url, headers=headers, data=data, stream=True).json()

    total = res["total"]
    print(total)
    data1(total)


def data1(total):
    list1 = []
    pages_int = int(int(total) / 100)
    # print(pages)
    # print(pages / 1000)
    if int(total) / 100 > 1:
        # print('大于1')
        if int(total) / 100 - pages_int > 0:
            pages_num = pages_int + 1
            # print('大于2', pages_num)
            list1.append(pages_num)
        else:
            pages_num = pages_int
            # print('整数',pages_num)
            list1.append(pages_num)
    else:
        pages_num = 1
        # print('小于1',pages_num)
        list1.append(pages_num)
    list_ts = []
    # num = 0
    # jim=open('jim.csv','w',encoding='utf-8')

    for j in range(list1[0]):
        # data_duo(j)
        ts = threading.Thread(target=data_duo, args=(j,))
        list_ts.append(ts)

    for i_ts1 in list_ts:
        i_ts1.start()  # 启动线程

    for i_ts in list_ts:
        i_ts.join()  # 关闭线程


def data_duo(j):
    url = 'http://sw.eccang.com/transfer/service-transfer-plan-order/list/page/{}/pageSize/100'.format(j)
    res = requests.post(url, headers=headers, data=data, stream=True).json()
    for i in res["data"]:
        dierceng = i["E0"]
        fba_head_id = i["ftpo_code"]
        fba_head_plan = i["E1"]
        fbaid = i["E3"]
        tpid = i["E33"]
        user_account = i["stpo_user_account"]
        logistics_method = i["sm_code"]
        plan_time = i["E28"]
        deliver_time = i["E9"]
        actual_deliver_time = i["E31"]
        reach_time = i["E17"]
        memo = i["E22"]
        all1 = (fba_head_id, fba_head_plan, fbaid, tpid, user_account, logistics_method, plan_time, deliver_time,
                actual_deliver_time, reach_time, memo)
        # print(all1)
        # jim.write(f'{all1}\n')
        data2(dierceng, all1)
        # print('111111111111111111111111111111111111111')
        # try:
        #     data2(dierceng, all1)
        # except Exception as g:
        #     data2(dierceng, all1)


def data2(dierceng, all1):
    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cache-Control': 'no-cache',
        'Cookie': 'UM_distinctid=17c15b16b7275-02b55430879ff9-2343360-1fa400-17c15b16b7376d; _ga=GA1.2.368327117.1632464693; LANGUAGE=zh_CN; PHPSESSID=s4rspra1rp4omskbsk55krte96; currentPage=4669%7B%7C%7D%2Ftransfer%2Fservice-transfer-plan-order%2Fdetail%2Fid%2F4669%7B%7C%7D%E5%8F%91%E8%B4%A7%E8%AE%A1%E5%88%92%E8%AF%A6%E6%83%85; SERVERID=c2e5cdc45974b52df44ef6e66ee49d8b|1638254127|1638159244',
        'Host': 'sw.eccang.com',
        'Pragma': 'no-cache',
        'Proxy-Connection': 'keep-alive',
        'Referer': 'http://sw.eccang.com/?company_code=sw',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
    }
    # url2 = 'http://sw.eccang.com/transfer/service-transfer-plan-order/detail/id/4664'
    url2 = 'http://sw.eccang.com/transfer/service-transfer-plan-order/detail/id/{}'.format(dierceng)
    res = requests.get(url=url2, headers=headers, stream=True).content.decode()
    # print(res)
    # res_html=etree.HTML(res)
    # all=res_html.xpath("//tbody[@id='selected-data']/tr")
    # sel = parsel.Selector(res)
    # datas = sel.css('#selected-data >tr')
    # for data in datas:
    #     warehouse_sku = data.css('.ec-center td ::text').get().strip()
    #     # print(warehouse_sku)
    #     product_name = data.css('[align=left]::text')[1].get().strip()
    #     # print(product_name)
    #     prod_trans_attr =  'prod_trans_attr暂无' if data.css('.ec-center::text')[2].get().strip()==' ' else data.css('.ec-center::text')[2].get().strip()
    #     # print(prod_trans_attr)
    #     isconcat = 'isconcat暂无' if data.css('.ec-center::text')[-3].get()=='' else data.css('.ec-center::text')[-3].get().strip()
    #     # print(isconcat)
    #
    #     fnsku = data.css('.stpodThirdBarcode ::text').get()
    #     # print(fnsku)
    #     plan_num = data.css('.ec-center::text')[-2].get()
    #     # print(plan_num)
    #     actual_num = data.css('.ec-center::text')[-1].get()
    #     # print(actual_num)
    #     diff = data.css('td font::text').get()
    # print(222222222222222222222222222222222222222222222)
    selector = parsel.Selector(res)
    trs = selector.css('#selected-data > tr')
    for tr in trs:
        # 产品代码
        warehouse_sku = tr.css('.ec-center tr td:nth-child(2)::text').get()
        # 产品名称
        product_name = tr.css('td:nth-child(3)::text').get()
        # 产品物流属性
        prod_trans_attr = tr.css('.ec-center:nth-child(4)::text').get()
        # 是否组合
        isconcat = tr.css('.ec-center:nth-child(5)::text').get()
        # 海外仓产品代码
        fnsku = tr.css('.stpodThirdBarcode::text').get()
        # 计划数
        plan_num = tr.css('.ec-center:nth-child(7)::text').get()
        # 实际发货数
        actual_num = tr.css('.ec-center:nth-last-child(2)::text').get()
        # 差异
        diff = tr.css('.ec-center:nth-last-child(1) font::text').get()

        # print(diff)
        # print('**********************')

        # product_name=all[i].xpath(f"//tbody[@id='selected-data']/tr[@class='table-module-b{i+1}']/td[3]")[0].text
        # # print(product_name)
        # isconcat=all[i].xpath(f"//tbody[@id='selected-data']/tr[@class='table-module-b{i+1}']/td[@class='ec-center']")[2].text
        # # print(isconcat)
        # fnsku=all[i].xpath(f"//tbody[@id='selected-data']/tr[@class='table-module-b{i+1}']/td[@class='stpodThirdBarcode']")[0].text
        # # print(fnsku)
        # plan_num=all[i].xpath(f"//tbody[@id='selected-data']/tr[@class='table-module-b{i+1}']/td[@class='ec-center']")[3].text
        # # print(plan_num)
        # actual_num=all[i].xpath(f"//tbody[@id='selected-data']/tr[@class='table-module-b{i+1}']/td[@class='ec-center']")[4].text
        # # print(actual_num)
        # diff=all[i].xpath(f"//tbody[@id='selected-data']/tr[@class='table-module-b{i+1}']/td[@class='ec-center']/font")[0].text
        # # print(diff),

        # plan_num = None if plan_num == None or plan_num == ' ' else plan_num == int(plan_num.strip())
        # actual_num = None if actual_num == None or actual_num == ' ' else actual_num == int(actual_num.strip())
        # diff = None if diff == None or diff == ' ' else diff == int(diff.strip())
        print(all1,warehouse_sku, product_name, prod_trans_attr, isconcat, fnsku, plan_num, actual_num, diff)
        # print(3333333333333333333333333333333333333)
        lock.acquire()
        mysql(all1, warehouse_sku, product_name, prod_trans_attr, isconcat, fnsku, plan_num, actual_num, diff)
        lock.release()

def mysql(all1, warehouse_sku, product_name, prod_trans_attr, isconcat, fnsku, plan_num, actual_num, diff):
    one1 = None if str(all1[6][:10]) == '0000-00-00' else datetime.datetime.strptime(
        str(all1[6]).replace('-', '').replace(':', '').replace(' ', ''), "%Y%m%d%H%M%S")
    one2 = None if str(all1[7][:10]) == '0000-00-00' else datetime.datetime.strptime(
        str(all1[7]).replace('-', '').replace(':', '').replace(' ', ''), "%Y%m%d")
    one3 = None if str(all1[8][:10]) == '0000-00-00' else datetime.datetime.strptime(
        str(all1[8]).replace('-', '').replace(':', '').replace(' ', ''), "%Y%m%d%H%M%S")
    one4 = None if str(all1[9][:10]) == '0000-00-00' else datetime.datetime.strptime(
        str(all1[9]).replace('-', '').replace(':', '').replace(' ', ''), "%Y%m%d")

    # one2=datetime.datetime.strptime(str(all1[7]).replace('-','').replace(':','').replace(' ',''), "%Y%m%d")
    # one3=datetime.datetime.strptime(str(all1[8]).replace('-','').replace(':','').replace(' ',''), "%Y%m%d%H%M%S")
    # one4=datetime.datetime.strptime(str(all1[9]).replace('-','').replace(':','').replace(' ',''), "%Y%m%d")
    # plan_num = None if plan_num == None or plan_num == ' ' else plan_num == int(plan_num.strip())
    # actual_num = None if actual_num == None or actual_num == ' ' else actual_num == int(actual_num.strip())
    # diff = None if diff == None or diff == ' ' else diff == int(diff.strip())
    zhujian=str(str(all1[1])+str(warehouse_sku))


    cursor.execute(
        "REPLACE into det_fba_first_shipment_product set fba_head_id=%s,fba_head_plan=%s,fbaid=%s,tpid=%s,user_account=%s,logistics_method=%s,plan_time=%s,deliver_time=%s,actual_deliver_time=%s ,reach_time=%s,warehouse_sku=%s,product_name=%s,prod_trans_attr=%s,isconcat=%s,fnsku=%s,plan_num=%s,actual_num=%s,diff=%s,memo=%s,zhujian=%s",
        [all1[0], all1[1], all1[2], all1[3], all1[4], all1[5], one1, one2,
         one3, one4, warehouse_sku, product_name, prod_trans_attr, isconcat, fnsku, plan_num, actual_num
            , diff, all1[10],zhujian])

    conn.commit()


if __name__ == '__main__':
    data0()
